﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FindJPG
{
    /// <summary>
    /// 找到C盘所有jpg文件
    /// </summary>
    
    class Program
    {
        //初始化总文件数
        static int count = 0;
        static void Main(string[] args)
        {
            string path = "C:/";
            FindJPG(path);
            //输出总jpg数目
            Console.WriteLine(count);
            Console.ReadKey();
        }
        static void FindJPG(string path)
        {
            //如果path为空，则结束递归
            if (path == null)
                return;
            try
            {
                //找到path目录下所有子文件以及jpg文件，如果文件下还有其他文件则进一步搜索
                var subpath = System.IO.Directory.EnumerateDirectories(path);
                var subjpg = Directory.EnumerateFileSystemEntries(path, "*.jpg");
                foreach (string jpg in subjpg)
                {
                    Console.WriteLine(jpg);
                    count++;
                }
                //通过递归将所有文件夹遍历
                foreach (var sub in subpath)
                    FindJPG(sub);
            }
            //错误路径则继续
            catch 
            {
                return;    
            }
        }
    }
}
